/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.panel;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import org.balisunrise.swing.components.panel.layouts.EntityGridLayoutBuilder;

/**
 *
 * @author Glauco
 */
public class GridPanel extends JPanel{

    private JTable table;
    private JScrollPane scrollPane;

    private EntityGridLayoutBuilder layoutBuilder;

    public GridPanel(JTable table, EntityGridLayoutBuilder layoutBuilder) {
        super();
        this.table = table;
        this.layoutBuilder = layoutBuilder;
        createScrollPane();
    }

    public EntityGridLayoutBuilder getLayoutBuilder() {
        return layoutBuilder;
    }

    public void setLayoutBuilder(EntityGridLayoutBuilder layoutBuilder) {
        this.layoutBuilder = layoutBuilder;
    }

    public JTable getTable() {
        return table;
    }

    public void setTable(JTable table) {
        this.table = table;
        createScrollPane();
    }

    public JScrollPane getScrollPane() {
        return scrollPane;
    }

    public void setScrollPane(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
        createLayout();
    }

    private void createScrollPane() {

        if (table == null)
            return ;

        // cria Scroll
        JScrollPane scroll = new JScrollPane(table);
        String name = table.getName() + ":scrollPane";
        scroll.setName(name);
        scroll.getAccessibleContext().setAccessibleName(name);

        // adciona o table ao scrollPane
        scroll.setViewportView(table);
        //scroll.getViewport().setSize(table.getPreferredSize());
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        this.scrollPane = scroll;

        createLayout();
    }

    private void createLayout(){
        layoutBuilder.setComponent(scrollPane);
        layoutBuilder.buildLayout(this);
    }

}
